package com.test.service;


import com.test.dao.HistoryOrderDao;
import com.test.dao.HistoryOrderHaveDao;
import com.test.dao.MerchantDao;
import com.test.domain.HistoryOrder;
import com.test.domain.HistoryOrderFull;
import com.test.domain.HistoryOrderHave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HistoryOrderService {

    private final HistoryOrderDao historyOrderDao;

    private final HistoryOrderHaveDao historyOrderHaveDao;

    private final MerchantDao merchantDao;


    @Autowired
    public HistoryOrderService(HistoryOrderDao historyOrderDao, HistoryOrderHaveDao historyOrderHaveDao, MerchantDao merchantDao) {
        this.historyOrderDao = historyOrderDao;
        this.historyOrderHaveDao = historyOrderHaveDao;
        this.merchantDao = merchantDao;
    }

    public List<HistoryOrderFull> getHistoryOrderList(String customerPhoneNumber){
        List<HistoryOrderFull> historyOrderList=new ArrayList<>();

        List<String> historyOrderNumList = historyOrderDao.getHistoryOrderNumListByPhoneNumberC(customerPhoneNumber);
        for (String historyOrderNum : historyOrderNumList) {
            List<HistoryOrderHave> dishList = historyOrderHaveDao.getDishListByHistoryOrderNumber(historyOrderNum);
            HistoryOrder historyOrder = historyOrderDao.getHistoryOrderByHistoryOrderNumber(historyOrderNum);
            String shopName = merchantDao.getMerchantByPhoneNumber(historyOrder.getMerchantPhoneNumber()).getShopName();
            historyOrderList.add(new HistoryOrderFull(shopName,historyOrder,dishList) );
        }

        return historyOrderList;
    }

    public List<HistoryOrderFull> getHistoryOrderListM(String merchantPhoneNumber){
        List<HistoryOrderFull> historyOrderList=new ArrayList<>();

        List<String> historyOrderNumList = historyOrderDao.getHistoryOrderNumListByPhoneNumberM(merchantPhoneNumber);
        for (String historyOrderNum : historyOrderNumList) {
            List<HistoryOrderHave> dishList = historyOrderHaveDao.getDishListByHistoryOrderNumber(historyOrderNum);
            HistoryOrder historyOrder = historyOrderDao.getHistoryOrderByHistoryOrderNumber(historyOrderNum);
            String shopName = merchantDao.getMerchantByPhoneNumber(historyOrder.getMerchantPhoneNumber()).getShopName();
            historyOrderList.add(new HistoryOrderFull(shopName,historyOrder,dishList) );
        }

        return historyOrderList;
    }

    public int markMerchant(String historyOrderNumber,Integer merchantOrderGrade){
        return historyOrderDao.updateGradeByHistoryNumber(historyOrderNumber,merchantOrderGrade);
    }

    public int markDish(String historyOrderNumber,Integer orderDishGrade,String dishNumber){
        return historyOrderHaveDao.updateGradeByHistoryNumberAndDishNumber(historyOrderNumber,orderDishGrade,dishNumber);
    }

}
